var page = 1;
var pagenum = 10;
var fi = 1
var url = {
	list: api.role.list,
	delete: api.role.delete,
	add:api.role.add,
	edit:api.role.edit
	//启禁用修改
}
var main = [[ //表头
		 {
			field: 'role_id',
			title: '角色ID',
			sort: true,
			fixed: 'left'
		}, {
			field: 'role_name',
			title: '名称',
			sort: true
		}, {
			field: 'area_name',
			width: 200,
			title: '区域',
			templet:'<div>{{d.area?d.area.area_name:"-"}}</div>'
		}, {
			title: '操作',
			width: 250,
			toolbar: '#operation',
			fixed: 'right'
		}
	]];
var data = {
	page:1,
	pagenum:10
};
var where_json={}
{if $Think.get.role_name}
	where_json.role_name=['like','%{$Think.get.role_name}%']
{/if}

data.where_json=JSON.stringify(where_json)
ajaxdriver(data)

function ajaxdriver(data) {
	console.log(data)
	Load()
	ajax(url.list, data, 'post').done(function(res) {
		console.log('done')
		Load(1)
		if(res.code == 1) {
			console.log(res)
			
			table.render({
				elem: '#driver',
				cellMinWidth: 80,
				cols: main,
				data: res.list,
				limit: data.pagenum,
				even: true
			});
			if(fi==1){
				 laypage.render({
				    elem: 'page'
				    ,count: res.count
					,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
					,theme: '#1E9FFF'
				    ,jump: function(obj, first){
				    	if(!first){
				       		page=obj.curr
	    					data.page=page
	    					pagenum=obj.limit
	    					data.pagenum=pagenum
	    					ajaxdriver(data)
    					}else{
    						
    					}
				    }
				  });
				  fi=2
				 }
			table.on('tool(driver)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				 if(layEvent === 'edit') { //编辑
					//do something
					window.location.href = " {:url('/html/system/role_add')}?id=" + data1.role_id
					//同步更新缓存对应的值

				}else if(layEvent === 'delete') { 
					layer.confirm('是否删除', function(index){
					    	Load();	
					    	ajax(api.role.delete,{
								ids:data1.role_id
							},'post').done((res)=>{
								console.log(res)
								if(res.code==1){
									Msg('删除成功')
									ajaxdriver(data)
								}else{
									Err(res.msg)
								}
							}).fail(function(err) {
								Err('请求失败')
							});
					    	
					      layer.close(index);
					    });
					//编辑
					
				}  else if(layEvent === 'look') {
					
					layer.open({
					  type: 1,
					  skin: 'layui-layer-rim', //加上边框
					  area: ['350px', '70%'], //宽高
					  content: `
						<div style='padding:10px'>
							<table  class="layui-hide" id="role_user" lay-filter="role_user"></table>
				  			<div id="page2"></div>
						</div>
					  `,
					  success:function(){
					  	var main2 = [
							[ //表头
								 {
									field: 'username',
									title: '账号',
									sort: true,
									fixed: 'left'
								}, {
									field: 'mobile',
									title: '手机号'
								}
							]
						];
						data2={
							page:1,
							pagenum:10,
					  		where_json:JSON.stringify({
								role_id:['in',data1.role_id]
							})
					  	}
						ajaxrole_user(data2)
						function ajaxrole_user(){
							Load()
							var fi2=1;
					  	ajax(api.user.list,data2,'post').done(function(res2) {
					  		
							table.render({
								elem: '#role_user',
								cellMinWidth: 80,
								cols: main2,
								data: res2.list,
								limit: data2.pagenum,
								even: true
							});
							Load(1)
							if(fi2==1){
							 laypage.render({
							    elem: 'page'
							    ,count: res.count
							    ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
							    ,jump: function(obj, first){
							    	if(!first){
							       		page=obj.curr
				    					data.page=page
				    					pagenum=obj.limit
				    					data.pagenum=pagenum
				    					ajaxdriver(data)
			    					}else{
			    						
			    					}
							    }
							  });
							  fi2=2
							 }	
						}).fail(function(err) {
							Err('请求失败')
						});
						}
					  }
					});
				} 
			});
			

		} else {
			Err(res.msg)
		}
	}).fail(function(err) {
		Err('请求失败')
	});

}

//添加
$('.add').on('click', function() {
	window.location.href = '{:url("/html/system/role_add")}'
});

